﻿#region Copyright (c) 2010 R. Uittenbosch
/*
* Copyright (C) 2010 R. Uittenbosch
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
* associated documentation files (the "Software"), to deal in the Software without restriction, including 
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
* copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
* following conditions:
* 
* The above copyright notice and this permission notice shall be included in all copies or substantial 
* portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Diac = System.Diagnostics;
using System.Windows.Forms;
using Draw = System.Drawing;

namespace TouchUI.System.Mobile.Mvc
{
  public class ScrollList : ScrollControl
  {
    public List<ScrollListItem> ListItems { get; set; }
    public int ListItemHeight { get; set; }
    public Brush HighBrush { get; set; }
    public Brush GrayBrush { get; set; }
    public int SelectedLine { get; set; }

    public ScrollList()
    {
      ListItems = new List<ScrollListItem>();
      SelectedLine = -1;
    }

    #region Events
    public event EventHandler Selected;
    protected virtual void OnSelected(EventArgs e)
    {
      if (Selected != null)
        Selected(this, e);
    }
    #endregion


    private List<int> locations = new List<int>();

    /// <summary>
    /// Check the consistency of the locations list.
    /// </summary>
    private void CheckConsistency()
    { 
      if (locations.Count == ListItems.Count)
        return;

      locations.Clear();
     
      int next = 0;
      foreach (ScrollListItem i in ListItems)
      {
        locations.Add(next);
        next += i.Height == 0? ListItemHeight: i.Height;
      }

    }


    public void MoveToItem(string name)
    {      
      int index = ListItems.FindIndex(i => (string.IsNullOrEmpty(i.Name)?-1:i.Name.CompareTo(name)) == 0);
      if (index != -1)
        ScrollTop = -locations[index];
    }

    public override void Paint(Graphics g)
    {
      CheckConsistency();

      DoAutomaticMotion();

      int firstListItem = Math.Max(locations.FindLastIndex(delegate(int x) { return x <= -(ScrollTop) ; }),0);
      int lastListItem  = Math.Min(locations.FindLastIndex(delegate(int x) { return x <  -(ScrollTop) + Rectangle.Height ; }) + 2,ListItems.Count);
    
      for (int i = firstListItem; i < lastListItem; i++)
      {
        ScrollListItem item = ListItems[i] as ScrollListItem;
        if (i == SelectedLine)
        {
          item.Selected = true;
          g.DrawRoundedRectangle(new Pen(Color.LightGray) { DashStyle = Draw.Drawing2D.DashStyle.Dash }, SystemColors.Highlight, new Rectangle(0, Rectangle.Top + locations[i] + ScrollTop, Rectangle.Width, item.Height), new Size(24, 24));

          //g.FillRectangle(HighBrush, new Rectangle(0, Rectangle.Top + locations[i] + ScrollTop, Rectangle.Width, item.Height ));
        }
        else
        {
          item.Selected = false;
          if (i % 2 == 1)
            g.DrawRoundedRectangle(new Pen(Color.LightGray) { DashStyle = Draw.Drawing2D.DashStyle.Dash }, Color.White, new Rectangle(0, Rectangle.Top + locations[i] + ScrollTop, Rectangle.Width, item.Height), new Size(24, 24));

           // g.FillRectangle(GrayBrush, new Rectangle(0, Rectangle.Top + locations[i] + ScrollTop, Rectangle.Width, item.Height));

        }
        item.Rectangle = new Rectangle(0, Rectangle.Top + locations[i] + ScrollTop, Rectangle.Width, item.Height);
        item.Paint(g);
        // Set the next item location
        if (i + 1 < locations.Count)
          locations[i + 1] = locations[i] + item.Height;
      }
    }

    public override void Resize(Rectangle r)
    {
      Rectangle = r;
      base.Resize(r);
    }

    public override void MouseUp(Point p)
    {
      base.MouseUp(p);

      if (PointSelected)
      {
        int y = p.Y - ScrollTop;
        int curItem = Math.Min(locations.FindLastIndex(delegate(int x) { return x < y; }), ListItems.Count);
        if (SelectedLine != curItem)
        {
          SelectedLine = curItem;
          OnSelected(EventArgs.Empty);
        }
      }
    }
  }
}
